﻿using BaseUI;
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class SelectCondition : BasePage
    {
        private Lc_ConditionBLL lcb = new Lc_ConditionBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();
        private Lc_QueueBLL lqb = new Lc_QueueBLL();
        private Lc_NodeBLL lnd = new Lc_NodeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProcess();

                if (Session["CondBufferMemory1"] != null)
                {
                    ddlProcess.SelectedValue = Session["CondBufferMemory1"].ToString();
                }
                if (Session["CondBufferMemory2"] != null)
                {
                    ddlState.SelectedValue = Session["CondBufferMemory2"].ToString();
                }
                Bind();
            }
        }

        private void BindProcess()
        {
            DataTable dt = lpb.GetAllInfo("", "");
            ddlProcess.DataSource = dt;
            ddlProcess.DataValueField = "N_PROCESS_ID";
            ddlProcess.DataTextField = "C_PROCESS_NAME";
            ddlProcess.DataBind();
            ListItem item1 = new ListItem("---请选择---", "-1");
            ddlProcess.Items.Insert(0, item1);
            ddlProcess.SelectedValue = "-1";
        }

        private void Bind()
        {
            string whereStr = "";
            string key = ddlProcess.SelectedValue.ToString();
            string STATE = ddlState.SelectedValue.ToString();

            if (key != "-1")
            {
                whereStr += " AND p.N_PROCESS_ID= '" + key + "'";
            }
            if (STATE != "-1")
            {
                whereStr += " AND t.N_CONDITION_STATE = '" + STATE + "'";
            }
            ViewState["Where"] = whereStr;

            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            //string whereStr = "";
            //string key = ddlProcess.SelectedValue.ToString();
            //string STATE = ddlState.SelectedValue.ToString();

            //if (key != "-1")
            //{
            //    whereStr += " AND p.N_PROCESS_ID= '" + key + "'";
            //}
            //if (STATE != "-1")
            //{
            //    whereStr += " AND t.N_CONDITION_STATE = '" + STATE + "'";
            //}
            //ViewState["Where"] = whereStr;

            //---------------------
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = lcb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = lcb.GetListByPage(Where, "N_CONDITION_ID", PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (ddlProcess.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }

            Bind();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Session["CondBufferMemory1"] = ddlProcess.SelectedValue.ToString();
            Session["CondBufferMemory2"] = ddlState.SelectedValue.ToString();
            MessageBox.ResponseScript(this, "FORM_INSERT();");
            //Response.Redirect("InsertCondition.aspx");
        }

        protected void BtnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        Session["CondBufferMemory1"] = ddlProcess.SelectedValue.ToString();
                        Session["CondBufferMemory2"] = ddlState.SelectedValue.ToString();

                        string pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        //string url = "EditCondition.aspx?ID=" + pid;
                        //Response.Redirect(url);
                        return;
                    }
                }
            }
        }

        protected void btnDelele_Click(object sender, EventArgs e)
        {
            //string ids = "";
            List<decimal> ids = new List<decimal>();
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    string id = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                    ids.Add(decimal.Parse(id));
                    //ids += id + ",";
                }
            }

            if (ids.Count == 0)
            {
                MessageBox.Show(Page, "请选择要删除的逻辑条件");
                return;
            }
            else
            {
                int count = lcb.DELETE(ids
                    .ToArray());
                if (count > 0)
                {
                    MessageBox.Show(Page, "删除成功");
                    Bind();
                    return;
                }
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;

            rowcount = lcb.GetRecordCount(Where);

            DataTable dtItem;

            dtItem = lcb.GetListByPage(Where, "N_CONDITION_ID", PageIng.CurrentPage, PageIng.RowsPerPage);

            DataView view = dtItem.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}